package yagwl.client.view.demo;

import yagwl.client.model.ItemDTO;
import yagwl.client.model.demo.LaureateListCache;
import yagwl.client.widget.PagedCellTable;
import yagwl.client.widget.Section;
import yagwl.shared.demo.LaureateConstants;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ImageCell;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;

public class LaureateListView2 extends Composite {
  private final Section section = new Section("Nobel Laureates");
  private final LaureateListCache listCache = new LaureateListCache();
  private final PagedCellTable pagedTable = new PagedCellTable();
  private final CellTable<ItemDTO> cellTable;

  public final static String HistoryToken = "laureateList2";

  public LaureateListView2() {
    listCache.setHistoryToken(HistoryToken);
    section.setDescription(new HTML(desc));
    section.setFootnote("Data from wikipedia; parsing errors possible.");
    cellTable = pagedTable.getCellTable();
    initCellTableColumns();
    initButtonBar();
    initSortOrderOptions();
    initPageSizeOptions();
    pagedTable.setListCache(listCache);
    section.setContent(pagedTable);
    initWidget(section);
  }

  private void initButtonBar() {
    pagedTable.addButton("Some Action", new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        pagedTable.setMessage("Button clicked");
      }
    });
  }

  private void initPageSizeOptions() {
    pagedTable.setPageSizeOptions(new int[] { 20, 50 });
  }

  private void initSortOrderOptions() {
    pagedTable.addSortOrderOption(LaureateConstants.SortYearAsc,
        "Earliest First");
    pagedTable.addSortOrderOption(LaureateConstants.SortYearDesc,
        "Latest First");
    pagedTable.addSortOrderOption(LaureateConstants.SortCountryAsc,
        "Country Ascending");
    pagedTable.addSortOrderOption(LaureateConstants.SortCountryDesc,
        "Country Descending");
    pagedTable.addSortOrderOption(LaureateConstants.SortCatAscYearAsc,
        "Category Asc, Earliest First");
    pagedTable.addSortOrderOption(LaureateConstants.SortCatAscYearDesc,
        "Category Asc, Latest First");
    pagedTable.addSortOrderOption(LaureateConstants.SortCatDescYearAsc,
        "Category Desc, Earliest First");
    pagedTable.addSortOrderOption(LaureateConstants.SortCatDescYearDesc,
        "Category Desc, Latest First");
    pagedTable.setSortOrderOptionIndex(0);
  }

  private void initCellTableColumns() {
    // Year
    TextColumn<ItemDTO> yearColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("year");
      }
    };
    cellTable.addColumn(yearColumn, "Year");

    // Category
    TextColumn<ItemDTO> catColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("category");
      }
    };
    cellTable.addColumn(catColumn, "Category");

    // Photo
    ImageCell imageCell = new ImageCell();
    Column<ItemDTO, String> photoColumn = new Column<ItemDTO, String>(imageCell) {
      @Override
      public String getValue(ItemDTO item) {
        String p = item.get("photo");
        if (p != null && p.length() != 0)
          return p;
        return "/img/trpx.gif";
      }
    };
    cellTable.addColumn(photoColumn, "Photo");

    // Name
    LinkCell linkCell = new LinkCell();
    Column<ItemDTO, String> nameColumn = new Column<ItemDTO, String>(linkCell) {
      @Override
      public String getValue(ItemDTO item) {
        String a = "<a href=http://en.wikipedia.org" + item.get("nameLink")
            + " target=_blank>" + item.get("name") + "</a>";
        return a;
      }
    };
    cellTable.addColumn(nameColumn, "Name");

    // Country
    TextColumn<ItemDTO> conColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("country");
      }
    };
    cellTable.addColumn(conColumn, "Country");

    // Rationale
    TextColumn<ItemDTO> ratColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("rationale");
      }
    };
    cellTable.addColumn(ratColumn, "Rationale");
  }

  private class LinkCell extends AbstractCell<String> {

    // @Override
    // public void render(String value, Object key, SafeHtmlBuilder sb) {
    // sb.appendHtmlConstant(value);
    // }

    @Override
    public void render(Context context, String value, SafeHtmlBuilder sb) {
      sb.appendHtmlConstant(value);
    }
  }

  private static final String desc = "PagedCellTable builds on GWT CellTable and GWT SimplePager, "
      + "and adds sorting options, page size options and table-level action buttons.<p/>";
}